package com.kehao.leetcode.hard.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class CalculateSolution {

    //理论上这个方法的时间复杂度应该就是O（n），但是不知道为什么超时了
    public int calculate01(String s) {
        if(s==null||s.length()==0) return 0;

        List<String> list = new ArrayList<>();
        char[] charArray = s.toCharArray();
        String unitStr = "";
        for (int i = 0; i < charArray.length; i++) {
            if(charArray[i]>='0'&&charArray[i]<='9'){
                unitStr+=charArray[i];
            }else if(charArray[i]=='+'||charArray[i]=='-'||charArray[i]=='*'||charArray[i]=='/'){
                list.add(unitStr);
                list.add(charArray[i]+"");
                unitStr = "";
            }
        }
        list.add(unitStr);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).equals("*")){
                int answer = Integer.parseInt(list.get(i-1))*Integer.parseInt(list.get(i+1));
                list.remove(i+1);
                list.remove(i);
                list.remove(i-1);
                list.add(i-1,answer+"");
                i--;
            }else if(list.get(i).equals("/")){
                int answer = Integer.parseInt(list.get(i-1))/Integer.parseInt(list.get(i+1));
                list.remove(i+1);
                list.remove(i);
                list.remove(i-1);
                list.add(i-1,answer+"");
                i--;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).equals("+")){
                int answer = Integer.parseInt(list.get(i-1))+Integer.parseInt(list.get(i+1));
                list.remove(i+1);
                list.remove(i);
                list.remove(i-1);
                list.add(i-1,answer+"");
                i--;
            }else if(list.get(i).equals("-")){
                int answer = Integer.parseInt(list.get(i-1))-Integer.parseInt(list.get(i+1));
                list.remove(i+1);
                list.remove(i);
                list.remove(i-1);
                list.add(i-1,answer+"");
                i--;
            }
        }

        return Integer.parseInt(list.get(0));
    }

    public int calculate(String s) {
        if(s==null||s.length()==0) return 0;
        int result = 0;
        Stack<Integer> stack = new Stack<>();
        char[] charArray = s.toCharArray();
        int num = 0;
        char preSign = '+';
        for (int i = 0; i < charArray.length; i++) {
            if(charArray[i]>='0'&&charArray[i]<='9'){
                num= num*10 + charArray[i]-'0';
            }
            if(charArray[i]=='+'||charArray[i]=='-'||charArray[i]=='*'||charArray[i]=='/'||i==charArray.length-1){
                switch (preSign){
                    case '+':
                        stack.push(num);
                        break;
                    case '-':
                        stack.push(-num);
                        break;
                    case '*':
                        stack.push(num*stack.pop());
                        break;
                    case '/':
                        stack.push(stack.pop()/num);
                        break;
                }
                num = 0;
                preSign = charArray[i];
            }
        }
        while (!stack.isEmpty()){
            result+=stack.pop();
        }
        return result;
    }

    @Test
    public void test01(){
        String s = "3+2*2";//7
        int result = calculate(s);
        System.out.println(result);
    }

    @Test
    public void test02(){
        String s = " 3/2 ";//1
        int result = calculate(s);
        System.out.println(result);
    }

    @Test
    public void test03(){
        String s = " 3+5 / 2 ";//5
        int result = calculate(s);
        System.out.println(result);
    }

    @Test
    public void test(){
        String s = "";
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            s = s+i;
            list.add(s);
        }
        list.stream().forEach(str-> System.out.println(str));
    }


}
